package com.easy.easy.Service.impl;

import com.easy.easy.Service.ICartService;
import com.easy.easy.bean.Cart;
import com.easy.easy.dao.CartDao;
import com.easy.easy.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class CartServiceimpl implements ICartService {

    @Autowired
    CartDao cartDao;

    @Override
    public int add(Cart cart) {
        Cart cart_data=getCart(cart);
        if(cart_data==null){
            cart.setNum(1);
            return cartDao.add(cart);
        }else{
            cart.setNum(cart_data.getNum()+1);
            return cartDao.edit(cart);
        }
    }

    @Override
    public Cart edit(Cart cart) {
        int number=cart.getNum();
        if(number<=0){
            cart.setNum(1);
            cartDao.edit(cart);
            throw new RuntimeException("购物车数量不能小于1");
        }
        cartDao.edit(cart);
        return cart;
    }

    @Override
    public int delete(Cart cart) {
        return cartDao.delete(cart);
    }

    @Override
    public Cart getCart(Cart cart) {
        return cartDao.getCart(cart);
    }

    @Override
    public List<Cart> getCarts(int userid) {
        return cartDao.getCarts(userid);
    }


}
